package com.lz.ionicbase.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lz.ionicbase.R;
import com.lz.ionicbase.utils.Constant;
import com.lz.ionicbase.utils.SpUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


public class SetServerActivity extends BaseActivity {
    private static final String TAG = SetServerActivity.class.getSimpleName();
    @InjectView(R.id.customTitle_tv) TextView customTitle_tv;
    @InjectView(R.id.right_bt) Button right_bt;

    @InjectView(R.id.serverip_et)
    protected EditText serverip_et;
    @InjectView(R.id.port_et)
    protected EditText port_et;

    private Context context;
    private String serverip = "";
    private String port = "";

    @Override
    protected int getActivityLayout() {
        return R.layout.activity_server;
    }

    @Override
    protected void initView() {
        context = this;
        ButterKnife.inject(this);

        customTitle_tv.setText(R.string.set_server);
        right_bt.setVisibility(View.VISIBLE);

        serverip = SpUtils.getString(this, Constant.SERVERIP,"");
        port = SpUtils.getString(this, Constant.PORT,"");

        serverip_et.setText(serverip);
        port_et.setText(port);
    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.my_back,R.id.right_bt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.right_bt:
                onTest();
//                onNext();
                break;
            case R.id.my_back:
                onBackPressed();
                break;
        }
    }

    private boolean checkIP(String str) {
        String regEx = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
        Pattern pattern = Pattern.compile(regEx);
        // 忽略大小写的写法
        // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }
    private boolean checkDomain (String str) {
        String regEx = "^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$";
        Pattern pattern = Pattern.compile(regEx);
        // 忽略大小写的写法
        // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    private void onTest() {
        //112.125.95.20
        //8585
        //u1   sa

//        String ss = Locale.SIMPLIFIED_CHINESE.getLanguage() +"#"+Locale.SIMPLIFIED_CHINESE.getCountry()
//        ;
//        Log.e(TAG, ss);

        Intent intent = new Intent(this, BaseWebViewActivity.class);
//        intent.putExtra("url","file:///android_asset/ceshi2.html");
//        intent.putExtra("url","file:///android_asset/windowTest1.htm");
//        intent.putExtra("url","http://112.125.95.20:8585/project/html/notice2.jsp");
//        intent.putExtra("url","http://112.125.95.20:8585/project/mobile/notice/list.jsp");
//        intent.putExtra("url","https://view.officeapps.live.com/op/view.aspx?src=http://www.pcu.ac.kr/chinese/images/edu_appli2.doc");
//        intent.putExtra("url","https://view.officeapps.live.com/op/view.aspx?src=http%3A%2F%2Fmba.ustc.edu.cn%2Fimg%2F20111104024721921.doc");
        intent.putExtra("url","http://mba.ustc.edu.cn/img/20111104024721921.doc");

        startActivity(intent);
    }
    private void onNext() {
        serverip = serverip_et.getText().toString().trim();
        port = port_et.getText().toString().trim();
//        if(!checkIP(serverip)){
//            Toast.makeText(this,"主机地址不正确!",Toast.LENGTH_SHORT).show();
//            return;
//        }

        if(TextUtils.isEmpty(serverip)||TextUtils.isEmpty(port)){
            Toast.makeText(this,"端口,主机地址不能为空",Toast.LENGTH_LONG).show();
            return;
        }

        if(!checkDomain(serverip) && !checkIP(serverip)){
            Toast.makeText(this,"主机地址不正确!",Toast.LENGTH_SHORT).show();
            return;
        }

        SpUtils.saveString(this, Constant.SERVERIP, serverip);
        SpUtils.saveString(this, Constant.PORT, port);

        Toast.makeText(this,"服务器地址已保存",Toast.LENGTH_LONG).show();

        String baseUrl = "http://"+ serverip +":"+ port;
        SpUtils.saveString(this, SpUtils.KEY_BASE_URL,baseUrl);

        setResult(RESULT_OK);
        finish();
    }

}
